package com.njtoyo.taxi.admin.logic.driver;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.njtoyo.taxi.admin.mapper.master.entity.DriverAuditInfo;
import com.njtoyo.taxi.admin.mapper.master.entity.DriverAuditLog;
import com.njtoyo.taxi.admin.mapper.master.entity.DriverExtraInformation;
import com.njtoyo.taxi.admin.mapper.master.mapper.DriverAuditInfoMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.DriverAuditLogMapper;
import com.njtoyo.taxi.admin.mapper.secondary.entity.AdminUser;
import com.njtoyo.taxi.admin.mapper.secondary.mapper.AdminUserMapper;
import com.njtoyo.taxi.admin.rest.presenter.business.driver.DriverAuditLogPresenter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import static java.util.stream.Collectors.toMap;
import static java.util.stream.Collectors.toSet;

@Service
public class DriverAuditLogic {
    @Autowired
    private DriverAuditLogMapper driverAuditLogMapper;

    @Autowired
    private AdminUserMapper adminUserMapper;

    @Autowired
    private DriverAuditInfoMapper driverAuditInfoMapper;

    public void saveLog(DriverAuditLog auditLog) {
        driverAuditLogMapper.insert(auditLog);
    }

    public List<DriverAuditLogPresenter> selectByAuditId(Long auditId) {
        List<DriverAuditLogPresenter> res = new ArrayList<>();

        List<DriverAuditLog> list = new LambdaQueryChainWrapper<>(driverAuditLogMapper)
                .eq(DriverAuditLog::getDriverAuditId, auditId)
                .orderByDesc(DriverAuditLog::getId)
                .list();

        if (ObjectUtil.isNotEmpty(list)) {
            // 查管理员姓名
            Set<Integer> adminIds = list.stream().map(DriverAuditLog::getAdminId).collect(toSet());

            List<AdminUser> adminUserList = adminUserMapper.selectBatchIds(adminIds);
            Map<Integer, String> adminUserMap = null;
            if (ObjectUtil.isNotEmpty(adminUserList)) {
                adminUserMap = adminUserList.stream().collect(toMap(AdminUser::getId, AdminUser::getName));
            }

            for (DriverAuditLog i : list) {
                DriverAuditLogPresenter item = BeanUtil.copyProperties(i, DriverAuditLogPresenter.class);

                if (Objects.nonNull(adminUserMap)) {
                    item.setAdminName(adminUserMap.getOrDefault(i.getAdminId(), ""));
                }

                res.add(item);
            }
        }

        return res;
    }

    // 审核注册通过，计算3种state
    public DriverExtraInformation getStateForAdopt(DriverAuditInfo info) {
        DriverExtraInformation res = new DriverExtraInformation();
        // 行驶证
        String vehicleNormal = isNormal(info.getVehicleLicenceExpiringTimestamp());
        String vehicleUpdate = isUpdate(info.getVehicleLicenceExpiringTimestamp());
        String vehicleOverdue = isOverdue(info.getVehicleLicenceExpiringTimestamp());

        String qualificationNormal = isNormal(info.getQualificationCertificateExpiringTimestamp());
        String qualificationUpdate = isUpdate(info.getQualificationCertificateExpiringTimestamp());
        String qualificationOverdue = isOverdue(info.getQualificationCertificateExpiringTimestamp());

        String normal = String.format("%s%s", vehicleNormal, qualificationNormal);
        String update = String.format("%s%s", vehicleUpdate, qualificationUpdate);
        String overdue = String.format("%s%s", vehicleOverdue, qualificationOverdue);

        res.setNormalState(Integer.parseInt(normal, 2));
        res.setUpdateState(Integer.parseInt(update, 2));
        res.setOverdueState(Integer.parseInt(overdue, 2));

        return res;
    }

    private String isNormal(Integer timestamp) {
        Date today = new Date();
        long todayTime = DateUtil.beginOfDay(today).getTime();
        int todayTimeStamp = (int) (todayTime / 1000);

        long time = DateUtil.beginOfDay(DateUtil.offsetDay(today, 7)).getTime();
        int endTimeStamp = (int) (time / 1000);

        return endTimeStamp < timestamp ? "1" : "0";
    }

    private String isUpdate(Integer timestamp) {
        Date today = new Date();
        long todayTime = DateUtil.beginOfDay(today).getTime();
        int todayTimeStamp = (int) (todayTime / 1000);

        long time = DateUtil.beginOfDay(DateUtil.offsetDay(today, 7)).getTime();
        int endTimeStamp = (int) (time / 1000);

        return endTimeStamp >= timestamp && timestamp >= todayTimeStamp ? "1" : "0";
    }

    private String isOverdue(Integer timestamp) {
        Date today = new Date();
        long todayTime = DateUtil.beginOfDay(today).getTime();
        int todayTimeStamp = (int) (todayTime / 1000);

        return timestamp < todayTimeStamp ? "1" : "0";
    }

    public List<DriverAuditInfo> selectListByIds(List<Long> ids) {
        return new LambdaQueryChainWrapper<>(driverAuditInfoMapper)
                .in(DriverAuditInfo::getId, ids)
                .list();
    }
}
